package com.huobao.yunying.activitys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.ab.view.sliding.AbSlidingTabView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.DetailCart_data;
import com.huobao.yunying.entity.DetailGoodsData;
import com.huobao.yunying.entity.DetailStoreData;
import com.huobao.yunying.entity.Detail_images;
import com.huobao.yunying.entity.Detailprops;
import com.huobao.yunying.fragments.Tuwen_detail_Fragment;
import com.huobao.yunying.fragments.Tuwen_pinglun_Fragment;
import com.huobao.yunying.fragments.Tuwen_shuxing_Fragment;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 图文详情
 * 
 * @author Administrator
 *
 */
public class Act_Tuwenxiangqing extends HuobaoBaseActivity implements OnClickListener {

	@ViewInject(R.id.rtly_top)
	private RelativeLayout rtly_top;

	@ViewInject(R.id.tabview)
	private AbSlidingTabView mAbSlidingTabView;

	/** 图片列表 */
	private List<Detail_images> _images;
	/** 属性列表 */
	private List<Detailprops> props;
	/** 商品id */
	private String goodsid;
	private DetailGoodsData goodsdata;

	// 加入进货车按钮
	@ViewInject(R.id.tv_jinhuozhe)
	private TextView tv_jinhuozhe;
	// 联系按钮
	@ViewInject(R.id.tv_tel)
	private TextView tv_tel;
	// 立即订购按钮
	@ViewInject(R.id.tv_dinggou)
	private TextView tv_dinggou;
	@ViewInject(R.id.tv_save)
	private TextView tv_save;
	// 店铺数据
	private DetailStoreData storeData;
	// 购物车
	private DetailCart_data cartData;

	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_tuwenxiangqing;
	}

	/**
	 * 显示没有进货车的View
	 */
	private void showNoJinhuocheView() {
		tv_jinhuozhe.setVisibility(View.INVISIBLE);
		tv_tel.setVisibility(View.INVISIBLE);
		tv_dinggou.setText("咨询代理");
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		storeData = (DetailStoreData) getIntent().getSerializableExtra("storeData");
		cartData = (DetailCart_data) getIntent().getSerializableExtra("cartData");
		goodsdata = (DetailGoodsData) getIntent().getSerializableExtra("data");
		_images = (List<Detail_images>) getIntent().getSerializableExtra("images");
		props = (List<Detailprops>) getIntent().getSerializableExtra("props");
		goodsid = getIntent().getStringExtra("id");
		int top = goodsdata.is_collect == 0 ? R.mipmap.icon7 : R.mipmap.icon7_press;
		tv_save.setCompoundDrawablesWithIntrinsicBounds(0, top, 0, 0);
		String description = getIntent().getStringExtra("description");
		rtly_top.setOnClickListener(this);
		mAbTitleBar.setVisibility(View.GONE);
		// 缓存数量
		mAbSlidingTabView.getViewPager().setOffscreenPageLimit(5);
		mAbSlidingTabView.setTabTextColor(Color.BLACK);
		mAbSlidingTabView.setTabSelectColor(Color.rgb(30, 168, 131));
		mAbSlidingTabView.setTabBackgroundResource(R.drawable.tab_bg);
		mAbSlidingTabView.setTabLayoutBackgroundResource(R.mipmap.slide_top);
		mAbSlidingTabView.setTabPadding(20, 8, 20, 8);
		List<String> tabTexts = new ArrayList<String>();
		tabTexts.add("图文详情");
		tabTexts.add("商品属性");
		tabTexts.add("商品评论");
		List<Fragment> mFragments = new ArrayList<Fragment>();
		mFragments.add(Tuwen_detail_Fragment.getInstant(_images));
		mFragments.add(Tuwen_shuxing_Fragment.getInstant(props, description));
		mFragments.add(Tuwen_pinglun_Fragment.getInstant(goodsid));
		mAbSlidingTabView.addItemViews(tabTexts, mFragments);
		mAbSlidingTabView.setCurrentItem(1);
		mAbSlidingTabView.getViewPager().setCurrentItem(1);
		mAbSlidingTabView.setTabPadding(20, 8, 20, 8);
		if (goodsdata.genre.equals("3")) {
			showNoJinhuocheView();
		}
		tv_tel.setOnClickListener(this);
		tv_save.setOnClickListener(this);
		tv_jinhuozhe.setOnClickListener(this);
		tv_dinggou.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rtly_top:
			finish();
			break;
		case R.id.tv_tel:
			onTelClick();
			break;
		case R.id.tv_save:
			save();
			break;
		case R.id.tv_jinhuozhe:// 加入购物车按钮
			Intent in1 = new Intent(this, Act_Select_GuigeAndNum.class);
			in1.putExtra("data", goodsdata);
			in1.putExtra("gouwuche", cartData.cart_num);
			startActivity(in1);
			break;
		case R.id.tv_dinggou:
			if (goodsdata.genre.equals("3")) {
				Intent intent = new Intent(this, Act_Zixuan_Daili.class);
				intent.putExtra("id", goodsdata.goods_id);
				intent.putExtra("sid", storeData.store_id);
				startActivity(intent);
			} else {
				in1 = new Intent(this, Act_Select_GuigeAndNum.class);
				in1.putExtra("data", goodsdata);
				in1.putExtra("gouwuche", cartData.cart_num);
				startActivity(in1);
			}
			break;
		}
	}

	private void onTelClick() {
		if (storeData.tel == null)
			return;
		Uri uri = Uri.parse("tel:" + storeData.tel);
		Intent intent = new Intent(Intent.ACTION_DIAL, uri);
		startActivity(intent);
	}

	private void save() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "goods", HttpConst.SECOND_KEY, "goodscollect",
				"id", goodsdata.goods_id);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				Toast.makeText(context, jsonObject.getString("retval"), Toast.LENGTH_SHORT).show();
				tv_save.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.icon7_press, 0, 0);
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}
}
